package com.biz.primus.ms.product.service;

import com.biz.primus.base.vo.StockRespVo;
import com.biz.primus.base.vo.commodity.ProductStockVo;
import com.biz.primus.commodity.vo.CommodityVo;
import com.biz.primus.commodity.vo.frontend.RapidProductItemVo;
import com.biz.primus.common.enums.StockChannelEnum;
import com.biz.primus.common.utils.ValueUtils;
import com.biz.primus.model.stock.vo.StocksReqVo;
import com.biz.primus.ms.product.feign.StockFeignClient;
import com.biz.primus.ms.product.interfaces.ProductStockService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author jiangnan
 * @Description: TODO
 * @create 2017-10-12-下午3:49
 */
@Slf4j
@Service
public class ProductStockServiceImpl implements ProductStockService {

    @Autowired
    private StockFeignClient stockFeignClient;

    @Override
    public List<ProductStockVo> getStocks(List<CommodityVo> commodityVos, String depotCode) {
        List<String> productCodes = Lists.newArrayList();
        for (CommodityVo commodityVo : commodityVos) {
            if (commodityVo.getIsRapidProduct()) {
                for (RapidProductItemVo rapidProductItemVo : commodityVo.getRapidProductInfoVo()) {
                    productCodes.add(rapidProductItemVo.getProductCode());
                }
            } else {
                productCodes.add(commodityVo.getProductCode());
            }
        }
        StocksReqVo batchStocksReqVo = new StocksReqVo(depotCode, productCodes, StockChannelEnum.NORMAL);
        log.debug("getDepotsStockInBatch reqVo:{}", batchStocksReqVo);
        List<StockRespVo> depotsStockInBatch = stockFeignClient.getDepotsStockInBatch(batchStocksReqVo);
        log.debug("getDepotsStockInBatch respVo:{}", depotsStockInBatch);
        List<ProductStockVo> respVos = Lists.newArrayList();
        for (int i = 0; i < depotsStockInBatch.size(); i++) {
            ProductStockVo productStockVo = new ProductStockVo();
            if (commodityVos.get(i).getIsRapidProduct()) {
                StockRespVo groupStock = new StockRespVo();
                groupStock.setProductCode(commodityVos.get(i).getProductCode());
                Integer maxDepotStock = 0;
                Integer maxVirtualDepotStock = 0;
                for (RapidProductItemVo rapidProductItemVo : commodityVos.get(i).getRapidProductInfoVo()) {
                    Integer quantity = rapidProductItemVo.getQuantity();
                    if (depotsStockInBatch.get(i) != null && quantity != null && quantity != 0) {
                        maxDepotStock = Math.max(ValueUtils.getValue(maxDepotStock),
                                ValueUtils.getValue(Optional.ofNullable(depotsStockInBatch.get(i).getQuantity()).orElse(0) / quantity));

                        maxVirtualDepotStock = Math.max(ValueUtils.getValue(maxVirtualDepotStock),
                                ValueUtils.getValue(Optional.ofNullable(depotsStockInBatch.get(i).getWarehouseQuantity()).orElse(0) / quantity));
                    }
                    i++;
                }
                groupStock.setQuantity(maxDepotStock);
                groupStock.setWarehouseQuantity(maxVirtualDepotStock);
                productStockVo.setStockRespVo(groupStock);
            } else {
                productStockVo.setStockRespVo(depotsStockInBatch.get(i));
            }
            respVos.add(productStockVo);
        }
        return respVos;
    }


}
